﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using IO = System.IO;
using System.Windows.Shapes;
using Microsoft.Vbe.Interop;
using System.Xml;
using An_Im.Model;
using PPT = Microsoft.Office.Interop.PowerPoint;
using An_Im.UI;

namespace An_Im.Core
{
    static class CustomAnimationLib
    {
        /// <summary>
        /// 全部标签。
        /// </summary>
        public static IDictionary<string, Tag> TagDictionary => CoreManipulator.TagDictionary;

        /// <summary>
        /// 全部标签组。
        /// </summary>
        public static IDictionary<string, TagGroup> TagGroupDictionary => CoreManipulator.TagGroupDictionary;

        ///// <summary>
        ///// 自定义效果列表。
        ///// </summary>
        //public static ObservableCollection<EffectInformation> EffectInformationCollection { get; private set; } = new ObservableCollection<EffectInformation>();

        public static string ExportVBACode(EffectInformation effectInformation)
        {
            return CoreManipulator.CreateArtificialVBACode(effectInformation as ArtificialEffectInformation);
        }

        public static EffectInformation GetEffectInfomation(PPT.Effect eff, bool isNative)
        {
            if (eff == null) return null;
            var validName = eff.Shape.Name + eff.GetHashCode();
            validName = validName.Replace('-', '_');
            validName = validName.Replace(' ', '_');
            validName = validName.Replace(',', '_');
            validName = validName.TrimStart('_');
            var neweff = CoreManipulator.RecordArtificialEffect(eff, validName, null, null);
            return neweff;
        }

        public static IEnumerable<EffectInformation> GetEffectInfomations(IEnumerable<PPT.Effect> effs, bool isNative, IList<string> tags = null)
        {
            var efflist = new List<EffectInformation>();
            if (effs == null) return efflist;
            foreach (var eff in effs)
            {
                if (eff == null) continue;
                var validName = eff.Shape.Name + eff.GetHashCode();
                validName = validName.Replace('-', '_');
                validName = validName.Replace(' ', '_');
                validName = validName.Replace(',', '_');
                validName = validName.TrimStart('_');
                var neweff = CoreManipulator.RecordArtificialEffect(eff, validName, null, null);
                if (tags == null || tags.Count == 0)
                    neweff.Tags = new string[] { "默认标签" };
                else
                    neweff.Tags = tags;
                efflist.Add(neweff);
            }

            return efflist;
        }


        public static void Add(EffectInformation eff, bool update = true)
        {
            if(eff.Tags == null)
                TagDictionary["默认标签"].EffectInformationList.Add(eff);
            else
            {
                foreach (var tag in eff.Tags)
                {
                    TagDictionary[tag].EffectInformationList.Add(eff);
                }

            }

            if (update)
                ((TaskPaneManager.GetActiveTaskPaneGroup().AnRecorderPane.Control as AnImWPFElementControl).WPFUserControl as AnImRecorderControl).itemsControls_lv1.Items.Refresh();
        }

        public static void AddRange(IEnumerable<EffectInformation> effs)
        {
            foreach (var item in effs)
            {
                Add(item, false);
            }

            ((TaskPaneManager.GetActiveTaskPaneGroup().AnRecorderPane.Control as AnImWPFElementControl).WPFUserControl as AnImRecorderControl).itemsControls_lv1.Items.Refresh();
        }

        public static void Remove(EffectInformation animData)
        {
            foreach (var item in TagDictionary)
            {
                if (item.Value.EffectInformationList.Contains(animData))
                    item.Value.EffectInformationList.Remove(animData);
            }
        }

        public static IEnumerable<EffectInformation> GetEffectInformations()
        {
            var list = new List<EffectInformation>();
            foreach (var pair in TagDictionary)
            {
                list.AddRange(pair.Value.EffectInformationList);
            }
            return list;
        }

        public static void Save()
        {
            var xmldoc = new XmlDocument();
            var xmlname = CoreManipulator.SavePath;
            var root = xmldoc.CreateElement("动画数据");
            var animdata_art = xmldoc.CreateElement("动画数据列表");
            var animdata_nat = xmldoc.CreateElement("原生数据列表");
            var animdata_grp = xmldoc.CreateElement("组数据列表");


            foreach (var effinfo in GetEffectInformations())
            {
                XmlElement xmlele = null;
                if (effinfo is ArtificialEffectInformation)
                {
                    xmlele = CoreManipulator.ExportArtificialEffect(xmldoc, effinfo as ArtificialEffectInformation);
                    animdata_art.AppendChild(xmlele);
                }

                else if (effinfo is NativeEffectInformation)
                {
                    xmlele = CoreManipulator.ExportNativeEffect(xmldoc, effinfo as NativeEffectInformation);
                    animdata_nat.AppendChild(xmlele);
                }else if(effinfo is GroupEffectInformation)
                {
                    xmlele = CoreManipulator.ExportGroupEffect(xmldoc, effinfo as GroupEffectInformation);
                    animdata_grp.AppendChild(xmlele);
                }

                //animlist2.AppendChild(xmltempdoc.FirstChild.);
            }

            root.AppendChild(animdata_art);
            root.AppendChild(animdata_nat); 
            root.AppendChild(animdata_grp); 
            xmldoc.AppendChild(root);

            xmldoc.Save(xmlname);
        }
    }
}
